package stone.ast;

import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
import runner.Environment;
import stone.StoneException;

import java.util.List;

public class NegativeExpr extends ASTList {
  public NegativeExpr(List<ASTree> list) {
    super(list);
  }

  public ASTree operand() {
    return child(0);
  }

  @Override
  public String toString() {
    return "-" + operand();
  }

  @NotNull
  @Override
  public Object eval(@Nullable Environment env) {
    var v = operand().eval(env);
    if (v instanceof Integer) {
      return -(Integer) v;
    } else {
      throw new StoneException("bad type for -", this);
    }
  }
}
